// pages/myOrder/myOrder.js
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var type;
var token;
var status=99;

var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'my/order/exchange/list',
      data: { page: page, status: status },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          listItem: that.data.listItem.concat(res.data.data.data)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();

  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar: [
      { name: "全部", id: "99" },
      { name: "待发货", id: "1" },
      { name: "待收货", id: "2" },
      { name: "完成", id: "3" },
    ],
    activetab: 0,
    listItem: [],
    queS: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        token = res.data
        that.getLIst()
      },
    })
  },
  getLIst() {
    var that = this;
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    if (page > 1) {
      page--;
    }
    that.setData({
      queS: true
    })
    wx.request({
      url: url.URl + 'my/order/exchange/list',
      data: { page: page, status: status },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            listItem: res.data.data.data
          })
          wx.hideLoading()
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        } else {

          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  onChange: function (e) {
    var that = this;
    var index = e.detail.index;
    if (index == 0) {
      status = 99;
      page = 1;
    } if (index == 1) {
      status = 1;
      page = 1;
    } if (index == 2) {
      status = 2;
      page = 1;
    } if (index == 3) {
      status = 3;
      page = 1;
    }
    that.setData({
      listItem: []
    })
    that.getLIst()
  },
  remind: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/remind',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },

  confirm: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/exchange/confirm',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        that.getLIst()
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    page = 1;
    status = 99;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self = this;
    setTimeout(() => {
      // 模拟请求数据，并渲染
      page = 1;
      self.getLIst()
      // 数据成功后，停止下拉刷新
      wx.stopPullDownRefresh();
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})